<template>
  <div class="main">
    <section>
      <el-form :model="form" ref="form" label-width="100px" class="demo-ruleForm">
        <el-form-item label="标签名称:" prop="name" :rules="commonRules.notNullRule">
          <el-input :maxlength="10" v-model="form.name" placeholder="请输入标签名称,不超过10个字"></el-input>
        </el-form-item>
        <el-form-item label="标签图片:" prop="pic" :rules="commonRules.notNullRule">
          <!-- <myUpload v-model="form.pic" /> -->
          <el-upload
            class="avatar-uploader"
            :action="upladUrl"
            name="file"
            :show-file-list="false"
            :before-upload="beforeUpload"
            :on-success="uploadSuccess"
          >
            <img v-if="form.pic" :src="form.pic" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="标签描述:" prop="description">
          <el-input type="textarea" :maxlength="500" :rows="5" placeholder="请输入内容" v-model="form.description">
          </el-input>
        </el-form-item>
        <el-form-item label="是否启用:" prop="enableStatus">
          <el-switch :active-value="1" :inactive-value="0" v-model="form.enableStatus" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :loading="loading" @click="submitForm('ruleForm')">保存</el-button>
        </el-form-item>
      </el-form>

    </section>
  </div>
</template>

<script>
import { insertTag, updateTag } from '@/api/goodMiddleStage'
// import MyUpload from '@/components/Upload/myUpload'
export default {
  name: 'editTags',
  // components: {
  //   MyUpload
  // },
  data() {
    return {
      loading: false,
      form: {
        description: '',
        enableStatus: 0,
        name: '',
        pic: ''
      }
    }
  },
  created() {
    this.init()
  },
  methods: {
    init() {
      console.log(this.$route)
      this.form = this.$route.query
    },
    submitForm() {
      this.$refs['form'].validate(async valid => {
        if (valid) {
          try {
            this.loading = true
            let data = ''
            if (this.$route.name === 'goodsMiddleStage-tagsEdit') {
              data = await updateTag(this.form)
            } else {
              data = await insertTag(this.form)
            }
            if (data.message === 'SUCCESS') {
              this.loading = false
              this.$message.success('操作成功')
              this.$refs['form'].resetFields()
              this.$router.push({ path: '/goodsMiddleStage/tags' })
            } else {
              this.$message.success('操作失败')
            }
          } catch (error) {
            console.log(error)
          }
        } else {
          return false
        }
      })
    },
    uploadSuccess(res){
      this.$set(this.form, 'pic', res.filePath)
    }
  }
}
</script>
<style lang="scss">
.avatar-uploader {
  width: 148px;
  height: 148px;
  /deep/ .el-upload {
    width: 100%;
    height: 100%;
    line-height: 148px;
  }
  .avatar {
    width: 100%;
    height: 100%;
  }
}
</style>
